#include <string.h>
#include <stdio.h>
#include <zconf.h>
#include "rtp_user.h"
#include "Debug.h"
#include "rtp_unit.h"

static int RTPUser_udp_bind(int *s_socket, struct sockaddr_in *s_in, char *s_ip, unsigned short s_port) {
    const int on = 1;
    int ret = RTPUSER_SOCKET_SUCCEED;
    memset(s_in, 0, sizeof(struct sockaddr_in));

    s_in->sin_family = AF_INET;
    if (s_ip != 0)
        s_in->sin_addr.s_addr = inet_addr(s_ip);        //保存IP
    if (s_port != 0)
        s_in->sin_port = htons(s_port);                 //保存端口

    //创建发送套接字
    if (s_ip != 0 && s_port != 0) {
        //申请套接字
        *s_socket = socket(AF_INET, SOCK_DGRAM, 0);
        if (*s_socket != -1) {
            //端口复用
            setsockopt(*s_socket, SOL_SOCKET, SO_REUSEADDR, (const void *) &on, sizeof(on));
            //设置广播属性
            if (setsockopt(*s_socket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
                close(*s_socket);
                ret = RTPUSER_SOCKET_SET_ERR;
            } else {
                //绑定端口
                if (bind(*s_socket, (struct sockaddr *) s_in, sizeof(struct sockaddr_in)) == -1) {
                    close(*s_socket);
                    ret = RTPUSER_SOCKET_BIND_ERR;
                } else {
                    ret = RTPUSER_SOCKET_SUCCEED;
                }
            }
        }
    } else {
        ret = RTPUSER_SOCKET_CREATE_ERR;
    }
    return ret;
}

int RTPUser_udp_init(RTPUser *user, char *s_ip, unsigned short srtp_port, unsigned short srtcp_port) {
    int ret;
    user->type = RTPUSER_TYPE_UDP;

    //绑定RTP发送套接字
    ret = RTPUser_udp_bind(&user->send_socket, &user->s_rtp_in, s_ip, srtp_port);
    if (ret != RTPUSER_SOCKET_SUCCEED) {
        return ret;
    }

    //绑定RTCP接收套接字
    ret = RTPUser_udp_bind(&user->recv_socket, &user->s_rtcp_in, s_ip, srtcp_port);
    if (ret != RTPUSER_SOCKET_SUCCEED) {
        close(user->send_socket);
        return ret;
    }
    return ret;
}

int RTPUser_udp_free(RTPUser *user) {
    if (user == 0)
        return 0;
    close(user->send_socket);
    close(user->recv_socket);
    return 0;
}

int RTPUser_tcp_init(RTPUser *user, TWListNode *rtp_unit, int c_socket, struct sockaddr_in *c_in) {
    user->rtpunit_node = rtp_unit;
    user->type = RTPUSER_TYPE_TCP;
    user->send_socket = c_socket;
    memcpy(&user->c_rtp_in, c_in, sizeof(struct sockaddr_in));
    user->recv_socket = c_socket;
    memcpy(&user->c_rtcp_in, c_in, sizeof(struct sockaddr_in));
    return RTPUSER_SOCKET_SUCCEED;
}

void RTPUser_state_set(RTPUser *user, unsigned char state) {

    if (user->state == RTPUSER_STATE_PALY && state == RTPUSER_STATE_PAUSE) {
        ((RTPUnit *) user->rtpunit_node->dataptr)->play_number--;
        user->state = state;
    } else if (user->state == RTPUSER_STATE_PALY && state == RTPUSER_STATE_CANCEL) {
        ((RTPUnit *) user->rtpunit_node->dataptr)->play_number--;
        user->state = state;
    } else if (user->state == RTPUSER_STATE_PAUSE && state == RTPUSER_STATE_PALY) {
        ((RTPUnit *) user->rtpunit_node->dataptr)->play_number++;
        user->state = state;
    } else if (user->state == RTPUSER_STATE_CANCEL) { ;
    } else {
        user->state = state;
    }

}

